# /path/to/project/manifests/lamp.pp

class update {
    # Backup the original apt sources list 
    # if the backup files does not exist
    exec { 'backup-source':
        command => '/bin/cp /etc/apt/sources.list /etc/apt/sources.list.ori',
        creates => '/etc/apt/sources.list.ori',
    }

    # Imports the local sources list
    file { '/etc/apt/sources.list':
        ensure => present,
        owner => root,
        group => root,
        source => '/vagrant/modules/base/sources.list',
    }

    # Do Apt update
    exec { 'apt-get update':
        command => '/usr/bin/apt-get update',
        require => File['/etc/apt/sources.list']
    }
}

class util {
    # Installs Curl and Vim
    $packages = [
        'curl',
        'vim'
    ]

    package { $packages:
        ensure => present,
        require => Exec['apt-get update']
    }
}

class apache {
    # Ensures Apache2 is installed
    package { 'apache2':
        ensure => present,
    }

    file { '/etc/apache2/sites-available/vagrant-web':
        ensure => present,
        source => '/vagrant/modules/apache/vagrant-web',
        require => Package['apache2']
    }

    file { '/etc/apache2/sites-enabled/000-default':
        ensure => link,
        target => '/etc/apache2/sites-available/vagrant-web',
        require => File['/etc/apache2/sites-available/vagrant-web']
    }

    # Ensures Apache service is running
    service { 'apache2':
        ensure => running,
        require => Package['apache2'],
        subscribe => File['/etc/apache2/sites-available/vagrant-web']
    }
}

class php5 {
    # Installs PHP and common modules
    $packages = [
        'php5',
        'php5-cli',
        'php5-mysql',
        'php5-dev',
        'php5-mcrypt',
        'php5-gd',
        'php5-curl',
        'php5-xdebug',
        'php-pear',
        'php-apc',
        'libapache2-mod-php5'
    ]

    package { $packages:
        ensure => present,
        require => Exec['apt-get update']
    }
}

class mysql {
    # Installs the MySQL server
    package { 'mysql-server':
        ensure => present,
        require => Exec['apt-get update']
    }

    # Ensures MySQL service is running
    service { 'mysql':
        ensure => running,
        require => Package['mysql-server']
    }

    # Reset the default root password for MySQL
    exec { 'set-mysql-password':
        unless  => 'mysql -uroot -proot',
        path    => ['/bin', '/usr/bin'],
        command => 'mysqladmin -uroot password root',
        require => Service['mysql'],
    }
}

include update
include util
include apache
include php5
include mysql